<template>
  <el-dialog :visible.sync="dialogOpen" title="出库" width="30%" center>
    <div style="display: flex;flex-direction: column;align-items: center">
      <span>拣货人员： <el-input v-model="sorter" size="small" style="width: 300px"
                            placeholder="请输入拣货人员名称"></el-input></span>
      <div style="width: 71%;margin-top: 20px
">
        发车照片：<span style="color: gray; font-size: 12px"
      >(粘贴复制即可上传图片)</span
      >
        <div class="div" style="display: flex; align-items: center">
          <div
              v-for="(v, i) in fileList"
              style="display: flex; align-items: center; margin-right: 10px"
          >
            <el-image
                :key="i"
                style="width: 50px; height: 50px"
                ref="preview"
                :src="host + v"
                :preview-src-list="[host + v]"
            >
            </el-image>
            <div @click="deletePaiedUrl(i)">
              <el-icon name="delete"></el-icon>
            </div>
          </div>
        </div>
      </div>
    </div>
    <span slot="footer">
         <el-button @click="dialogOpen=false">取 消</el-button>
        <el-button type="primary" @click="onConfirm">提 交</el-button>
      </span>
  </el-dialog>
</template>

<script>
import axios from "axios";
import store from "../../../store";

export default {
  name: "warehouseOut",
  data() {
    return {
      outId: "",
      sorter: "",
      fileList: [],
      dialogOpen: false,
      host: this.$imgHost,
    }
  },
  computed: {
    preImgs() {
      return this.fileList.map((e) => `${this.$imgHost}${e}`);
    },
  },
  created() {
    document.addEventListener('paste', this.handlePaste)
  },
  methods: {
    handleOpen(val) {
      this.outId = val.id
      this.sorter = ''
      this.fileList = []
      this.dialogOpen = true
    },
    handlePaste(event) {
      if (!this.dialogOpen) return;
      const clipboardData = event.clipboardData || window.clipboardData;
      if (clipboardData) {
        for (let i = 0; i < clipboardData.items.length; i++) {
          const item = clipboardData.items[i];

          if (item.kind === 'file' && item.type.startsWith('image/')) {
            const file = item.getAsFile();
            if (file) {
              this.uploadImage(file);
            }
          } else {
            this.$message.error('请拷贝图片!');
            break;
          }
        }
      }
    },
    deletePaiedUrl(i) {
      this.fileList.splice(i, 1)
    },
    uploadImage(imageFile) {
      const formData = new FormData();
      formData.append('file', imageFile);
      axios.post(`${this.$apiHost}/common/fileImgUrl`, formData, {
        headers: {
          'Content-Type': 'multipart/form-data',
          'token': store.state.token
        }
      })
          .then(response => {
            this.fileList.push(response.data.result[0]);
            // this.preSalesImgList.push('https://' + response.data.result[0]);
          })
          .catch(error => {
          });
    },
    onConfirm() {
      const data = {
        sorter: this.sorter,
        deliveryImg: this.fileList[0],
        id: this.outId,
        orderId: this.outId,
        status: 0
      }
      this.$api["orderOutNew"](data).then(res => {
            this.$message.success('提交成功！')
            this.dialogOpen = false
            this.$emit('addStatus', 1)
          }
      )
    },
  }
}
</script>

<style scoped lang="less">
/deep/ .el-dialog__header {
  background-color: #0f79e2;
  //border-top-left-radius: 20px;
  //border-top-right-radius: 20px;
}

/deep/ .el-dialog__title {
  color: #fff;
}

</style>